

** step 1: construction of relationship strengh measures
do RFS_relationship_construction.do

** step 2: regression analysis

 
use RFS_master_data, clear

 egen tt = group(st0_date)
	 
	 *******************
	 ** RELATIONSHIP ***
	 *******************
	 * merge in the relationship dummies in "RFS_relationship_construction.do"
	 drop if tt < 7 // drop first 6 time units (day here, months in the paper) because relationship strengh cannot be computed for those :
	 
	 * part A.1.: client's most important dealer
	 * part A.1.: client's most important dealer	 
	 * part A.1.: client's most important dealer
	 merge m:1 tt call coun using size_call_dealer1 //  "RFS_relationship_construction.do"
	 drop if _merge == 2
	 drop _merge
	 
		 merge m:1 tt call coun using size_call_dealer2 //  "RFS_relationship_construction.do"
	 drop if _merge == 2
	 drop _merge 
	 
	 * part A.2.: dealer's most important clients
	 * part A.2.: dealer's most important clients	 
	 * part A.2.: dealer's most important clients	 
	    merge m:1 tt call coun using size_coun_client1 //  "RFS_relationship_construction.do"
	    drop if _merge == 2
	    drop _merge 
		
		* second most important client of dealers
	    merge m:1 tt call coun using size_coun_client2 //  "RFS_relationship_construction.do"
        drop if _merge == 2
	    drop _merge 
		
		* third most important client of dealers
	    merge m:1 tt call coun using size_coun_client3 //  "RFS_relationship_construction.do"
        drop if _merge == 2
	    drop _merge 	 
		
** generate the flaggers for relationships:

cap drop flag*

gen flagC1 = 0 
replace flagC1 = 1 if 	rat1  < .

gen flagC2 = 0 
replace flagC2 = 1 if 	rat1  < . | 	rat2  < .

gen flagD1 = 0 
replace flagD1 = 1 if 	deal1  < .

gen flagD2 = 0 
replace flagD2 = 1 if 	deal1  < . | 	deal2  < .

gen flagD3 = 0 
replace flagD3 = 1 if 	deal1  < . | 	deal2  < . | 	deal3  < .
 
* in the paper (table 6)
 tab flagC1
/*
          0 |    833,394       70.45       70.45
          1 |    349,488       29.55      100.00
------------+-----------------------------------
      Total |  1,182,882      100.00 */
 tab flagC2
/*
          0 |    656,042       55.46       55.46
          1 |    526,840       44.54      100.00
------------+-----------------------------------
      Total |  1,182,882      100.00   */
 tab flagD1
 /*
          0 |  1,081,180       91.40       91.40
          1 |    101,702        8.60      100.00
------------+-----------------------------------
      Total |  1,182,882      100.00
 */
 tab flagD2
  /*
          0 |  1,005,653       85.02       85.02
          1 |    177,229       14.98      100.00
------------+-----------------------------------
      Total |  1,182,882      100.00
  */
 
 
**** REGRESSIONS REGRESSIONS REGRESSIONS REGRESSIONS REGRESSIONS
**** REGRESSIONS REGRESSIONS REGRESSIONS REGRESSIONS REGRESSIONS
**** REGRESSIONS REGRESSIONS REGRESSIONS REGRESSIONS REGRESSIONS
**** REGRESSIONS REGRESSIONS REGRESSIONS REGRESSIONS REGRESSIONS

  cap drop size
 gen size = log(quantity  )
  
   * save the t-statistics:
 	 mat UALL = J(5,5,1)
 
 	 local gg = 1

	     *global vars3 "    N_aver N_coun   N_3hour N_BuSe N_IDB   " //  N_aver N_coun   N_3hour N_BuSe N_IDB

		  global vars3 "    flagC1 flagC2      flagD1 flagD2 flagD3      " //  N_aver N_coun   N_3hour N_BuSe N_IDB

			 
			 
  *** SAME REGRESSION WITH INTERACTION TERMS:
    foreach j of global vars3 {
di "`j'"
qui {
 cap drop yy xx*
 gen yy = N_aver
 gen xx = size
 gen xxRelationSh = `j'   
 qui winsor2 xx , replace cuts(1 99)
  qui winsor2 yy , replace cuts(1 99)
  cap estimates drop f*
  
 	reghdfe yy  i.xxRelationSh#c.xx    , absorb(st0_date#instrumentid#activeZ      )    cluster(call   st0_date)
  estimates store ff1
   test 1.xxRelationSh#c.xx = 0.xxRelationSh#c.xx
   	    local gg2 = r(p)
        mat UALL[1,`gg'] = `gg2'
    
 	reghdfe yy  i.xxRelationSh#c.xx       , absorb(st0_date#instrumentid#activeZ  call      )    cluster(call   st0_date)
  estimates store ff2
   test 1.xxRelationSh#c.xx = 0.xxRelationSh#c.xx
     local gg2 = r(p)
        mat UALL[2,`gg'] = `gg2'
    
 	reghdfe yy  i.xxRelationSh#c.xx    , absorb(st0_date#instrumentid#activeZ  call coun#activeZ     )    cluster(call   st0_date)
  estimates store ff3
   test 1.xxRelationSh#c.xx = 0.xxRelationSh#c.xx
     local gg2 = r(p)
        mat UALL[3,`gg'] = `gg2'
 
 	reghdfe yy  i.xxRelationSh#c.xx       , absorb(st0_date#instrumentid#activeZ mm#call st0_date#coun#activeZ     )    cluster(call   st0_date)
  estimates store ff4
   test 1.xxRelationSh#c.xx = 0.xxRelationSh#c.xx
     local gg2 = r(p)
        mat UALL[4,`gg'] = `gg2'
 
   
 	reghdfe yy  i.xxRelationSh#c.xx     , absorb(st0_date#instrumentid#activeZ mm#call st0_date#coun#activeZ call#coun  )    cluster(call   st0_date)
  estimates store ff5
   test 1.xxRelationSh#c.xx = 0.xxRelationSh#c.xx
     local gg2 = r(p)
        mat UALL[5,`gg'] = `gg2'
 
	 local gg =  `gg' + 1
	}
  esttab ff*     ///
 , star("*" 0.1 "**" 0.05 "***" 0.01) b(%10.3f) t(%10.2f) scalars(N r2) sfmt(%10.3f)   replace
   
   
     esttab ff* using "RFSRelationship_`j'.tex" /*  using size_linear_`j'_DS2.tex using size_linear_`j'.tex  */   ///
  , star("*" 0.1 "**" 0.05 "***" 0.01) b(%10.3f) t(%10.2f) scalars(N r2) sfmt(%10.3f)   replace
  
 

	 
	 
}


    mat list UALL
 
	
		
		
		
	 
	 
	 